﻿var _APP_NAME = "Sale_Assist_";

/// Cookie ///

function addCookie(objName, objValue) {
    var str = objName + "=" + escape(objValue);
    document.cookie = str;
    //log("addCookie-->objName: " + objName);
}

function getCookie(objName) {
    var arrStr = document.cookie.split(";");
    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        if (temp[0] == objName)
            return unescape(temp[1]);
    }
}

function clearCookie() {
    var arrStr = document.cookie.split(";");
    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        addCookie(temp[0], '');
    }
}

/// Storage ///

function setstorage(objName, objValue) {
    objName = _APP_NAME + objName;
    var storage = window.localStorage;
    if (window.localStorage) {
        localStorage.setItem(objName, objValue);
    }
    else {
        //log('setstorage-->This browser does NOT support localStorage');
        addCookie(objName, objValue);
    }
}

function getstorage(objName) {
    objName = _APP_NAME + objName;
    var storage = window.localStorage;
    var result = '';
    if (window.localStorage) {
        for (var i = 0; i < storage.length; i++) {
            if (storage.key(i) == objName)
                result = storage.getItem(storage.key(i));
        }
    }
    else {
        //log('getstorage-->This browser does NOT support localStorage');
        getCookie(objName);
    }
    return result;
}

function clearstorage() {
    var storage = window.localStorage;
    //log('clearstorage-->clear all localStorage data ~ ~ ~');
    if (window.localStorage) {
        localStorage.clear();
    }
    else {
        //log('clearstorage-->This browser does NOT support localStorage');
        clearCookie();
    }
}

